package com.yssoft.controller.app.sales;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yssoft.entity.sales.TCustomer;
import com.yssoft.entity.sales.TCustomer2;
import com.yssoft.entity.sales.TCustomerAtt;
import com.yssoft.service.purchase.RecipientDetailService;
import com.yssoft.service.sales.CustomerService;
import com.yssoft.utils.AppUtil;
import com.yssoft.vo.TCustomerVo;

/**
 * @author 庞雷
 * @time 2018/5/19 下午 13:34
 * @客户管理控制器
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping("/app/customer")
public class CustomerController {
	
	private static Logger logger=Logger.getLogger(CustomerService.class);
	
	@Resource 
	private CustomerService customerService;
	
	/**
	 * 客户列表
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "toCustomList", method = RequestMethod.POST)
	public Object toCustomList(int currentPage, int pageSize, String customerName, HttpServletRequest request){
		String msg = "客户列表查询";
    	try{
    		List<Map<String,Object>> pageList = customerService.toCustomerList(currentPage, pageSize, customerName, request);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 客户详情查看
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "toCustomer", method = RequestMethod.POST)
	public Object toCustomer(Integer customerId){
		String msg = "客户详情查看";
    	try{
    		Map<String,Object> customer = customerService.queryCustomer(customerId);
        	return AppUtil.returnMapJson("1", msg+"成功!", customer);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 创建客户
	 */
	@ResponseBody
    @RequestMapping(value = "addCustomer", method = RequestMethod.POST)
	public Object addCustomer(TCustomer2 tCustomer2, HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IOException, DecoderException {
		String msg = "创建客户";
		try {
			customerService.addCustomer(tCustomer2, files, request);
			return AppUtil.returnMapJson("1", msg+"成功!", null);
		} catch (Exception e) {
			logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 编辑客户
	 */
	@ResponseBody
    @RequestMapping(value = "editCustomer", method = RequestMethod.POST)
	public Object editCustomer(TCustomer2 tCustomer2, HttpServletRequest request, @RequestParam("files") MultipartFile[] files,String[] oldFile) throws IOException, DecoderException {
		String msg = "编辑客户";
		try {
			customerService.editCustomer(tCustomer2, files, oldFile, request);;
			return AppUtil.returnMapJson("1", msg+"成功!", null);
		} catch (Exception e) {
			logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 删除客户
	 */
	@ResponseBody
    @RequestMapping(value = "deleteCustomer", method = RequestMethod.POST)
	public Object deleteCustomer(Integer customerId, HttpServletRequest request){
		String msg = "删除客户";
    	try{
    		String result = customerService.deleteCustomer(customerId,request);
    		if("0".equals(result)){
    			return AppUtil.returnListJson("1", msg+"成功!", null);
    		} else {
    			return AppUtil.returnListJson("0", "该客户已关联产生销售订单，无法删除，请删除订单后再进行删除操作", null);
    		}
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
			
}
